package com.bill.gaty.mfidelity.dao.impl;

 
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
 
import android.database.Cursor;
import android.util.Log;

import com.bill.gaty.mfidelity.beans.Compatibilite;
import com.bill.gaty.mfidelity.beans.Gtf;
import com.bill.gaty.mfidelity.beans.Key;
import com.bill.gaty.mfidelity.dao.SecurityDao;
import com.bill.gaty.mfidelity.util.DaoAbstract;
import com.bill.gaty.mfidelity.util.GtfConstantes;
import com.bill.gaty.mfidelity.util.Security;

public class SecurityDaoImpl extends DaoAbstract implements SecurityDao{

	public SecurityDaoImpl(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String getEncriptionKey() {
		// TODO Auto-generated method stub
		try{
		return findAll().get(0).getKey();
		}catch (Exception e) {
			// TODO: handle exception
			Log.e("getEncriptionKey_Exception",e.getMessage());
		}
		return "";
	}

	@Override
	public String getEncriptionAlgo() {
		// TODO Auto-generated method stub
		try{
			return findAll().get(0).getAlgo();
			}catch (Exception e) {
				// TODO: handle exception
				Log.e("getEncriptionAlgo_Exception",e.getMessage());
			}
			return "AES";
	}

	@Override
	public List<Gtf> getSecureListGtf(List<Gtf> gtfs) {
		// TODO Auto-generated method stub
		byte[] key = Security.decodeAESKeyBase64(getEncriptionKey());
		for (Gtf gtf : gtfs) {
			gtf.setJsonData(Security.encryptAES(key,  gtf.getJsonData().getBytes()));
			 if(gtf.getVotreNom()!=null)
				 gtf.setVotreNom(Security.encryptAES(key,  gtf.getVotreNom().getBytes()));
		 if(gtf.getNomDeVotreAmi()!=null)
			 gtf.setNomDeVotreAmi(Security.encryptAES(key,  gtf.getNomDeVotreAmi().getBytes()));
		 if(gtf.getReponse()!=null)
			 gtf.setReponse(Security.encryptAES(key,  gtf.getReponse().getBytes()));
	 
		}
		return gtfs;
	}
	 
	public  Gtf getSecureGtf(Gtf gtfs) {
		// TODO Auto-generated method stub
		byte[] key = Security.decodeAESKeyBase64(getEncriptionKey());
		 
			gtfs.setJsonData(Security.encryptAES(key,  gtfs.getJsonData().getBytes()));
			 if(gtfs.getVotreNom()!=null)
					 gtfs.setVotreNom(Security.encryptAES(key,  gtfs.getVotreNom().getBytes()));
			 if(gtfs.getNomDeVotreAmi()!=null)
				 gtfs.setNomDeVotreAmi(Security.encryptAES(key,  gtfs.getNomDeVotreAmi().getBytes()));
		    if(gtfs.getReponse()!=null)
			  gtfs.setReponse(Security.encryptAES(key,  gtfs.getReponse().getBytes()));
			  /*
			   * 									
			   */
		return gtfs;
	}
	@Override
	public List<Compatibilite> getSecureListCompatibilite(
			List<Compatibilite> compatibilites) {
		// TODO Auto-generated method stub
		byte[] key=Security.decodeAESKeyBase64(getEncriptionKey());
		for (Compatibilite compatibilite : compatibilites) {
			compatibilite.setElle(Security.encryptAES(key,  compatibilite.getElle().getBytes()));
			compatibilite.setLui(Security.encryptAES(key,  compatibilite.getElle().getBytes()));
			compatibilite.setResult(Security.encryptAES(key,  compatibilite.getResult().getBytes()));
					}
		return compatibilites;
	}
	public  Compatibilite  getSecureCompatibilite(
			 Compatibilite  compatibilites) {
		// TODO Auto-generated method stub
		byte[] key=Security.decodeAESKeyBase64(getEncriptionKey());
		 
			compatibilites.setElle(Security.encryptAES(key,  compatibilites.getElle().getBytes()));
			compatibilites.setLui(Security.encryptAES(key,  compatibilites.getElle().getBytes()));
			compatibilites.setResult(Security.encryptAES(key,  compatibilites.getResult().getBytes()));
					 
		return compatibilites;
	}
	 
	 
	public List<Key> findAll() {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_SECURITY, GtfConstantes.COLS_SECURITY, null, null, null, null, null);
		return   cursorKey(c) ;
		
	}
	 
	protected List<Key> cursorKey(Cursor c) {
		// TODO Auto-generated method stub
		if (c==null||c.getCount() == 0)
			return null;
 List<Key> keys=new ArrayList<Key>();
		  
 Key key = null;
		 while(c.moveToNext()){
			 
			 key = new   Key();
			 key.setKey(c.getString(GtfConstantes.NUM_COL_SECURITY_KEY));
			 key.setAlgo( c.getString(GtfConstantes.NUM_COL_SECURITY_ALGO));
			  
			 keys.add(key);
			 
		 }
		c.close();
 
		 
		return keys;
	}

	@Override
	public List<Gtf> getUnSecureListGtfFromSecureListGtf(List<Gtf> gtfs) {
		// TODO Auto-generated method stub
		byte[] key = Security.decodeAESKeyBase64(getEncriptionKey());
		for (Gtf gtf : gtfs) {
			gtf.setJsonData(Security.decryptAESNonBase64(key,  gtf.getJsonData()));
			gtf.setNomDeVotreAmi(Security.decryptAESNonBase64(key,  gtf.getNomDeVotreAmi()));
			gtf.setVotreNom(Security.decryptAESNonBase64(key,  gtf.getVotreNom()));
			gtf.setReponse(Security.decryptAESNonBase64(key,  gtf.getReponse()));
			
			Log.i("getUnSecureListGtfFromSecureListGtf", gtf.getId()+"");
		}
		return gtfs;
	}

	@Override
	public  Gtf getUnSecureGtfFromSecureGtf(Gtf gtfs) {
		// TODO Auto-generated method stub
		byte[] key = Security.decodeAESKeyBase64(getEncriptionKey());
		 
			gtfs.setJsonData(Security.decryptAESNonBase64(key,  gtfs.getJsonData()));
			if(null!=gtfs.getNomDeVotreAmi())gtfs.setNomDeVotreAmi (Security.decryptAESNonBase64(key,  gtfs.getNomDeVotreAmi()));
			if(null!=gtfs.getVotreNom())	gtfs.setVotreNom  (Security.decryptAESNonBase64(key,  gtfs.getVotreNom()));
			if(null!=gtfs.getReponse())	gtfs.setReponse   (Security.decryptAESNonBase64(key,  gtfs.getReponse()));
			
			Log.i("getUnSecureListGtfFromSecureListGtf", gtfs.getId()+"");
		 
		return gtfs;
	}
	@Override
	public List<Compatibilite> getUnSecureListCompatibiliteFromSecureListCompatibilite(
			List<Compatibilite> compatibilites) {
		// TODO Auto-generated method stub
		byte[] key=Security.decodeAESKeyBase64(getEncriptionKey());
		for (Compatibilite compatibilite : compatibilites) {
			compatibilite.setElle(Security.decryptAESNonBase64(key,  compatibilite.getElle()));
			compatibilite.setLui(Security.decryptAESNonBase64(key,  compatibilite.getElle()));
			compatibilite.setResult(Security.decryptAESNonBase64(key,  compatibilite.getResult()));
					}
		return compatibilites;
	}
	 
}
